package com.ficus.road.maintain.core.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

/**
 * @author siyuan.li
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class QueryFilterCommonDTO {

    @JsonProperty("#eq")
    private Object hashEq;

    @JsonProperty("#lt")
    private Object hashLt;

    @JsonProperty("#gt")
    private Object hashGt;

    @JsonProperty("#lte")
    private Object hashLte;

    @JsonProperty("#gte")
    private Object hashGte;

    @JsonProperty("$ne")
    private Object notEq;

    @JsonProperty("#contains")
    private String contains;

    @JsonProperty("#in")
    private List<Object> ins;

    @JsonProperty("#nin")
    private List<Object> nin;


    /**
     * @return 判断这个对象的值是否都为null或者空字符串
     * @throws IllegalAccessException
     */
    public static boolean isAllFieldNUll(QueryFilterCommonDTO queryFilterCommonDTO) throws IllegalAccessException {
        if (queryFilterCommonDTO == null) {
            return true;
        }
        for (Field field : queryFilterCommonDTO.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object object = field.get(queryFilterCommonDTO);
            if (object instanceof CharSequence) {
                if (!ObjectUtils.isEmpty(object)) {
                    return false;
                }
            } else {
                if (!Objects.isNull(object)) {
                    return false;
                }
            }
        }
        return true;
    }
}

